﻿/*
   Advanced Flash Engine in ActionScript 3.
   Copyright (C) 2009  Dean Gardiner, Taylor Lodge

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */ /*
   Expression's Used in this Document.

   ExpressionDescription
   KeySessionWhen Key is Down its a session, when key is released it breaks the session.
   SessionWhen Engine Opened Starts the Session, when Engine Closed it breaks the session.
   HitSessionWhen we hit something on the map this starts the Session, when We stop hitting something on the map this breaks the Session.

 */
package com.winmastergames.lolgame {
	/* =============Import Classes============= */
	import com.senocular.utils.KeyObject;
	import flash.ui.Keyboard;
	import flash.text.*;
	import flash.geom.*;
	import flash.display.*;
	import flash.events.*;
	import flash.utils.getTimer;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.ui.Keyboard;
	import com.adventuresinactionscript.collisions.HitTest;
	import com.kongregate.as3.client.KongregateAPI;
	import com.kongregate.as3.client.events.KongregateEvent;
	import com.winmastergames.lolgame.gameUtils;
	import com.winmastergames.gameassets.xperience;

	/* =============Main Class============= */
	public class mapDefault extends MovieClip {
		/* =============Public Variables============= */
		private var stageRef:Stage; //Reference for Stage Object.
		private var Key:KeyObject; //For Handling Key Presses
		private var KeyA:uint = 65; //uint code for "A"
		private var KeyS:uint = 83; //uint code for "S"
		private var KeyD:uint = 68; //uint code for "D"
		private var KeyW:uint = 87; //uint code for "W"
		private var speed:Number = 0.5; //Speed for map Movement.
		private var VX:Number = 0; //X Velocity for map Movement.
		private var VY:Number = 0; //Y Velocity for map Movement.
		private var friction:Number = 0; //Friction for map Movement.
		private var maxSpeed:Number = 5; //Positive Max Speed for map Movement.
		private var nMaxSpeed:Number = -5; //Negetive Max Speed for map Movement.
		var player_mc:MovieClip; //Reference for Player Class.
		var Speed:Number = 5; //Speed for map Movement.
		var tempSpeed:Number;
		/*
		   ===============Direction Variables===============
		   0 = Nothing
		   1 = Left
		   2 = Right
		   3 = Top
		   4 = Bottom
		   5 = Top, Left
		   6 = Top, Right
		   7 = Bottom, Left
		   8 = Bottom, Right
		   =================================================
		 */
		public var Dir:Number = 0; //Direction player is Moving (See above for Codes).
		public var StopDir:Number = 0; //Direction player hit the Wall (See above for Codes).
		var hittext:String = "hello"; //?
		public var hitbool:Boolean = false; //Have we Hit Wall.
		public var debugon:Boolean = false; //Debug Window On.
		public var debugpb:Number = 0; //Debug Window Key Hit this KeySession.
		var kongregate:KongregateAPI; //Kongregate API Reference.
		var sessionHitWallBool:Boolean = false; //Hit wall this Session.
		var sessionDebugWnBool:Boolean = false; //Opened Debug Wn This Session.
		var playerTarRef:player_ltarget;

		/* =============Class Constructor============= */
		public function mapDefault(stageRef:Stage, playermc:MovieClip, mainthis:Object, kongregateRef:KongregateAPI, ptarget:player_ltarget){
			this.stageRef = stageRef; //Set stage Reference from constructor to a variable for the whole class.
			playerTarRef = ptarget;
			Key = new KeyObject(stageRef); //Sets KeyObject.
			player_mc = playermc; //Sets Reference for player class.
			engine.debugtextfield = hittext; //Sets ?
			kongregate = kongregateRef; //Sets Kongregate Reference.
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true); //Add's an event listener for ENTER_FRAME.
		}

		/* =============Main Class Loop============= */
		public function loop(e:Event):void {
			for (var c:Number = 0; c <= xperience.level; c++){
				tempSpeed = (speed * 15 / 100);
				speed = tempSpeed + (tempSpeed * 15 / 100);
			}
			engine.debugtextfield = hittext; //Loops ?
			//htobj
			Speed = 10;
			if (engine.noclip == false){
				Speed = 5;
				var htobj:Rectangle = com.adventuresinactionscript.collisions.HitTest.hitTestObject(player_mc, this.walls, true, 255); //Hit Test Map Against Player.
				if (htobj != null){ //If we have hit something.
					hittext = "HIT!"; //Sets ?
					if (sessionHitWallBool == false){ //If we have hit the wall this session.
						kongregate.stats.submit("Hit Your First Wall", 1); //Send Achivement.
					} //End
					sessionHitWallBool = true; //Hit Wall this session true.
					hitbool = true; //Hit Something.
					var old_Dir:Number = Dir; //Temp Variable for this HitSession.
					switch (old_Dir){ //Switch Between Direction Codes (See Above).
						case 1: //Direction Code 1 (See Above).
							x -= 1; //Move Player away from Wall.
							playerTarRef.x += 1;
							StopDir = 1; //Set StopDir for KeyBreaking.
							break; //Break Case.
						case 2: //Direction Code 2 (See Above).
							x += 1; //Move Player away from Wall.
							playerTarRef.x -= 1;
							StopDir = 2; //Set StopDir for KeyBreaking.
							break; //Break Case.
						case 3: //Direction Code 3 (See Above).
							y -= 1; //Move Player away from Wall.
							playerTarRef.y += 1;
							StopDir = 3; //Set StopDir for KeyBreaking.
							break; //Break Case.
						case 4: //Direction Code 4 (See Above).
							y += 1; //Move Player away from Wall.
							playerTarRef.y -= 1;
							StopDir = 4; //Set StopDir for KeyBreaking.
							break; //Break Case.
						case 5: //Direction Code 5 (See Above).
							x -= 1; //Move Player away from Wall.
							playerTarRef.x += 1;
							y -= 1; //Move Player away from Wall.
							playerTarRef.y += 1;
							StopDir = 5; //Set StopDir for KeyBreaking.
							break; //Break Case.
						case 6: //Direction Code 6 (See Above).
							x += 1; //Move Player away from Wall.
							playerTarRef.x -= 1;
							y -= 1; //Move Player away from Wall.
							playerTarRef.y += 1;
							StopDir = 6; //Set StopDir for KeyBreaking.
							break; //Break Case.
						case 7: //Direction Code 7 (See Above).
							y += 1; //Move Player away from Wall.
							playerTarRef.y -= 1;
							x -= 1; //Move Player away from Wall.
							playerTarRef.x += 1;
							StopDir = 7; //Set StopDir for KeyBreaking.
							break; //Break Case.
						case 8: //Direction Code 8 (See Above).
							y += 1; //Move Player away from Wall.
							playerTarRef.y -= 1;
							x += 1; //Move Player away from Wall.
							playerTarRef.x -= 1;
							StopDir = 8; //Set StopDir for KeyBreaking.
							break; //Break Case.
					} //End If
				} else { //Else
					hittext = "Not HIT!"; //Set ?
					hitbool = false; //Return hitbool to false.
				} // End htobj != null
			}
			if (player.running == true){
				Speed *= 1.7;
			}
			if (gameUtils.getState() != "pausemenu" && engine.csopen == false && Key.isDown(KeyW) && !Key.isDown(KeyA) && !Key.isDown(KeyD) && !Key.isDown(KeyS) && StopDir != 3 && hitbool == false){
				/*
				   If KeyW is Down,
				   KeyA, KeyD, KeyS Isnt Down,
				   StopDir Doesnt Equal 3 and HitBool Equals false.
				 */
				y += Speed; //Move Map by Y at +'Speed'.
				playerTarRef.y -= Speed;
				Dir = 3; //Set Dir to 3.
				switch (StopDir){ //Switch StopDir Codes for UnSetting StopDir.
					case 1: //Direction Code 1 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 2: //Direction Code 2 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 4: //Direction Code 4 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 5: //Direction Code 5 (See Above).
						StopDir = 0; //StopDir Equals 0.
				}
			} else if (gameUtils.getState() != "pausemenu" && engine.csopen == false && Key.isDown(KeyA) && Key.isDown(KeyD) && StopDir != 5 && StopDir != 1 && StopDir != 3 && hitbool == false){
				/*
				   If KeyA and KeyD is Down,
				   StopDir Doesnt Equal 5, 1 or 3 and HitBool Equals false.
				 */
				switch (StopDir){ //Switch StopDir Codes for UnSetting StopDir.
					case 1: //Direction Code 1 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 2: //Direction Code 2 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 3: //Direction Code 3 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 4: //Direction Code 4 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 6: //Direction Code 6 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 7: //Direction Code 7 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 8: //Direction Code 8 (See Above).
						StopDir = 0; //StopDir Equals 0.
				} //else if (Key.isDown(KeyW) && Key.isDown(KeyA) && StopDir!=5 && StopDir!=1 && StopDir!=5&&hitbool==false){
			} else if (gameUtils.getState() != "pausemenu" && engine.csopen == false && Key.isDown(KeyW) && Key.isDown(KeyD) && StopDir != 6 && StopDir != 2 && StopDir != 3 && hitbool == false){
				/*
				   If KeyW and KeyD is Down,
				   StopDir Doesnt Equal 6, 2 or 3 and HitBool Equals false.
				 */
				y += Speed; //Move Map by Y at +'Speed'.
				x -= Speed; //Move Map by Y at -'Speed'.
				playerTarRef.y -= Speed;
				playerTarRef.x += Speed;
				Dir = 6; //'Dir' Equals Direction Code 6.
			} else if (gameUtils.getState() != "pausemenu" && engine.csopen == false && Key.isDown(KeyW) && Key.isDown(KeyA) && StopDir != 5 && StopDir != 1 && StopDir != 5 && hitbool == false){
				/*
				   If KeyW and KeyA is Down,
				   StopDir Doesnt Equal 5, 1 or 5 and HitBool Equals false.
				 */
				y += Speed; //Move Map by Y at +'Speed'.
				x += Speed; //Move Map by X at +'Speed'.
				playerTarRef.y -= Speed;
				playerTarRef.x -= Speed;
				Dir = 5; //'Dir' Equals Direction Code 5.
			} else if (gameUtils.getState() != "pausemenu" && engine.csopen == false && Key.isDown(KeyS) && Key.isDown(KeyA) && StopDir != 7 && StopDir != 1 && StopDir != 4 && hitbool == false){
				/*
				   If KeyS and KeyA is Down,
				   StopDir Doesnt Equal 7, 1 or 4 and HitBool Equals false.
				 */
				y -= Speed; //Move Map by Y at -'Speed'.
				x += Speed; //Move Map by X at +'Speed'.
				playerTarRef.y += Speed;
				playerTarRef.x -= Speed;
				Dir = 7; //'Dir' Equals Direction Code 7.
			} else if (gameUtils.getState() != "pausemenu" && engine.csopen == false && Key.isDown(KeyS) && Key.isDown(KeyD) && StopDir != 8 && StopDir != 2 && StopDir != 4 && hitbool == false){
				/*
				   If KeyS and KeyD is Down,
				   StopDir Doesnt Equal 8, 2 or 3 and HitBool Equals false.
				 */
				y -= Speed; //Move Map by Y at -'Speed'.
				x -= Speed; //Move Map by X at -'Speed'.
				playerTarRef.y += Speed;
				playerTarRef.x += Speed;
				Dir = 8; //'Dir' Equals Direction Code 8.
			} else if (gameUtils.getState() != "pausemenu" && engine.csopen == false && Key.isDown(KeyS) && !Key.isDown(KeyA) && !Key.isDown(KeyD) && !Key.isDown(KeyW) && StopDir != 4 && hitbool == false){
				/*
				   If KeyS is Down,
				   KeyA, KeyD and KeyW isn't down,
				   StopDir Doesnt Equal 4 and HitBool Equals false.
				 */
				y -= Speed; //Move Map by Y at -'Speed'.
				playerTarRef.y += Speed;
				Dir = 4; //'Dir' Equals Direction Code 4.
				switch (StopDir){ //Switch StopDir Codes for UnSetting StopDir.
					case 1: //Direction Code 1 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 2: //Direction Code 2 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 3: //Direction Code 3 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 5: //Direction Code 5 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 6: //Direction Code 6 (See Above).
						StopDir = 0; //StopDir Equals 0.
				}
			} else if (gameUtils.getState() != "pausemenu" && engine.csopen == false && Key.isDown(KeyD) && !Key.isDown(KeyA) && !Key.isDown(KeyS) && !Key.isDown(KeyW) && StopDir != 2 && hitbool == false){
				/*
				   If KeyD is Down,
				   KeyA, KeyS and KeyW isn't down,
				   StopDir Doesnt Equal 2 and HitBool Equals false.
				 */
				x -= Speed; //Move Map by X at -'Speed'.
				playerTarRef.x += Speed;
				Dir = 2; //'Dir' Equals Direction Code 2.
				switch (StopDir){ //Switch StopDir Codes for UnSetting StopDir.
					case 1: //Direction Code 1 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 3: //Direction Code 3 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 4: //Direction Code 4 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 5: //Direction Code 5 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 6: //Direction Code 6 (See Above).
						StopDir = 0; //StopDir Equals 0.
				}
			} else if (gameUtils.getState() != "pausemenu" && engine.csopen == false && Key.isDown(KeyA) && !Key.isDown(KeyD) && !Key.isDown(KeyS) && !Key.isDown(KeyW) && StopDir != 1 && hitbool == false){
				/*
				   If KeyA is Down,
				   KeyD, KeyS and KeyW isn't down,
				   StopDir Doesnt Equal 1 and HitBool Equals false.
				 */
				x += Speed; //Move Map by X at +'Speed'.
				playerTarRef.x -= Speed;
				Dir = 1; //'Dir' Equals Direction Code 2.
				switch (StopDir){ //Switch StopDir Codes for UnSetting StopDir.
					case 2: //Direction Code 2 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 3: //Direction Code 3 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 4: //Direction Code 4 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 5: //Direction Code 5 (See Above).
						StopDir = 0; //StopDir Equals 0.
					case 6: //Direction Code 6 (See Above).
						StopDir = 0; //StopDir Equals 0.
				}
			} else if (gameUtils.getState() != "pausemenu" && engine.csopen == false && Key.isDown(Key.CONTROL) && Key.isDown(Key.SHIFT)){
				/*
				   If KeyControl and KeyShift is Down.
				 */
				if (debugpb == 0){ //Debug Key Hasn't Been Hit this KeySession.
					if (debugon == false){ //Debug Isn't Opened.
						if (sessionDebugWnBool == false){ //Debug Window Not Opened this Session.
							kongregate.stats.submit("Opened Debug Window", 1); //Send Achivement.
						}
						sessionDebugWnBool = true; //Debug Window Opened this Session.
						debugon = true; //Debug Window Opened.
					} else if (debugon == true){ // Else Debug Window is Open.
						debugon = false;
					} //Turn Debugging Off.
				}
				;
				debugpb = 1; //Debug Key Has Been Hit this KeySession.
			} else { //Else Key Not Pressed.
				debugpb = 0; //Debug Key Has Not Been Hit this KeySession.
				speed = 5;
			}
		}
	}
}